package com.example.lots_of_demo.utils;

import com.example.lots_of_demo.bo.PermissionResBo;
import com.example.lots_of_demo.model.PermissionGroup;
import com.example.lots_of_demo.model.PermissionGroupEnum;
import com.example.lots_of_demo.model.RPermissionRes;
import com.example.lots_of_demo.vo.PermissionGroupVo;
import org.springframework.util.ObjectUtils;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

/**
 * @author : LZJ
 * @description : code for LZJ
 * @date : 2021/2/16
 */
public class PermissionGroupUtils {

    /**
     * 将权限组对象  转换成  前端所需要的 权限组对象
     * @param permissionGroups
     * @return
     */
    public static List<PermissionGroupVo> transferPermissionGroupVo(List<PermissionGroup> permissionGroups, List<PermissionResBo> permissionResBos) {
        List<PermissionGroupVo> result = new LinkedList<>();
        initPermissionGroupVo(result);
        permissionGroups.forEach(permissionGroup -> {
            if(ObjectUtils.isEmpty(permissionGroup.getParentCode())){
                PermissionGroupVo permissionGroupVo = new PermissionGroupVo();
                permissionGroupVo.setId(transferLongOpposite(permissionGroup.getId())).setLabel(permissionGroup.getDesc());
                List<PermissionGroupVo> children = new LinkedList<>();
                permissionGroups.forEach(temp -> {
                    if(!ObjectUtils.isEmpty(temp.getParentCode()) && permissionGroup.getCode().equals(temp.getParentCode())){
                        PermissionGroupVo tempPermissionGroupVo = new PermissionGroupVo();
                        List<PermissionGroupVo> resChildren = new LinkedList<>();
                        tempPermissionGroupVo.setId(transferLongOpposite(temp.getId())).setLabel(temp.getDesc());
                        permissionResBos.forEach(permissionResBo -> {
                            if(permissionResBo.getCode().equals(temp.getPermissionCode())){
                                permissionResBo.getRes().forEach(res -> {
                                    PermissionGroupVo resPermissionGroupVo = new PermissionGroupVo();
                                    resPermissionGroupVo.setId(res.getPermissionId()).setLabel(res.getDesc());
                                    resChildren.add(resPermissionGroupVo);
                                });
                            }
                        });
                        tempPermissionGroupVo.setChildren(resChildren);
                        children.add(tempPermissionGroupVo);
                    }
                });
                permissionGroupVo.setChildren(children);
                judgeAddToGroup(result, permissionGroup, permissionGroupVo);
            }
        });
        return result;
    }

    /**
     * 把长整型数字 翻转相反数
     */
    public static Long transferLongOpposite(Long number){
        return number * -1;
    }


    /**
     * 初始化PermissionGroupVo集合 往里面添加权限组的初始两大分组
     */
    private static void initPermissionGroupVo(List<PermissionGroupVo> permissionGroupVos){
        Arrays.stream(PermissionGroupEnum.PermissionGroupType.values()).forEach(type ->{
            PermissionGroupVo permissionGroupVo = new PermissionGroupVo();
            permissionGroupVo.setDisabled(true).setLabel(type.getDesc()).setChildren(new LinkedList<>());
            permissionGroupVos.add(permissionGroupVo);
        });
    }

    /**
     * 判断权限组 该添加到哪个 权限组总分组
     */
    private static void judgeAddToGroup(List<PermissionGroupVo> result, PermissionGroup judge, PermissionGroupVo target){
        String prefix = judge.getCode().substring(0, 3);
        if(PermissionGroupEnum.PermissionGroupType.COM.getCode().equals(prefix)){
            result.forEach(group -> {
                if (group.getLabel().equals(PermissionGroupEnum.PermissionGroupType.COM.getDesc())) {
                    group.getChildren().add(target);
                }
            });
        }else if(PermissionGroupEnum.PermissionGroupType.MGT.getCode().equals(prefix)){
            result.forEach(group -> {
                if (group.getLabel().equals(PermissionGroupEnum.PermissionGroupType.MGT.getDesc())) {
                    group.getChildren().add(target);
                }
            });
        }
    }

}
